const app = getApp();
var WxParse = require('../wxParse/wxParse.js');
Page({
  data: {
    key: '',
    username: '',
    department: '',
    employeeId: '',
    checkedall: false,
    disabled_submit: true,
    items: [],
    selectMemo: '',
    selectIdList: [],
  },
  onLoad(query) {
    // 页面加载
    dd.showLoading({
      content: '加载中...',
      delay: 1000,
    });
    var that = this;
    that.setData({
      key: app.globalData.key,
      username: app.globalData.employeeName,
      department: app.globalData.department,
      employeeId: app.globalData.employeeId,
      selectIdList: []
    });
    dd.httpRequest({
      url: app.globalData.doMainName + '/dingapi/workflow/getworkflowactive.ashx',
      method: 'POST',
      data: {
        key: that.data.key,
        employeeId: that.data.employeeId,
        flowType: query.flowType
      },
      dataType: 'json',
      async: false,
      success: function(res3) {
        dd.hideLoading();
        if (res3.data.items.length == 0) {
          dd.alert({ content: '暂无审批单据' });
        }
        else {
          that.setData({
            items: res3.data.items
          })
        }

        for (var i = 0; i < res3.data.items.length; i++) {
          for (var j = 0; j < res3.data.items[i].details.length; j++) {
            var article = res3.data.items[i].details[j].value;
            if (article.indexOf('</p>') >= 0) {
              WxParse.wxParse('article' + i, 'html', article, that);
              if (i == res3.data.items.length - 1) {
                WxParse.wxParseTemArray('array', 'article', res3.data.items.length, that);
              }
            }
          }
        }
        var obj_items = that.data.items;
        for (var i = 0; i < that.data.array.length; i++) {
          for (var j = 0; j < obj_items[i].details.length; j++) {
            var article = obj_items[i].details[j].value;
            if (article.indexOf('</p>') >= 0) {
              var obj = that.data.array[i];
              obj_items[i].details[j].array = obj;
            }
            if (article.indexOf('http://') >= 0) {
              //console.log(article);
            }
          }
        }
        that.setData({
          items: obj_items
        })
      },
      fail: function(err) {
        dd.alert({ title: JSON.stringify(err) });
      }
    });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    //dd.hideLoading();
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
  onAllCheckChange(e) {
    var that = this;
    var list = [];
    var items = that.data.items;
    if (e.detail.value == true) {
      for (var i = 0; i < items.length; i++) {
        items[i].check = true;
        list.push(items[i].id);
      }
    }
    else {
      for (var i = 0; i < items.length; i++) {
        items[i].check = false;
      }
    }
    that.setData({
      items: items,
      selectIdList: list
    });
    this.onDisabledSubmit();
  },
  onCheckChange(e) {
    var id = e.currentTarget.id;
    var that = this;
    var list = that.data.selectIdList;
    if (e.detail.value == true) {
      list.push(id);
    }
    else {
      for (var i = 0; i < list.length; i++) {
        if (id == list[i]) {
          list.splice(i, 1);
        }
      }
    }
    that.setData({
      selectIdList: list
    });
    this.onDisabledSubmit();
  },
  onDisabledSubmit() {
    var list = this.data.selectIdList;
    if (list.length == 0) {
      this.setData({
        disabled_submit: true,
        selectMemo: '',
        checkedall: false,
      })
    }
    else {
      this.setData({
        disabled_submit: false,
        selectMemo: '(' + list.length.toString() + ')'
      })
    }
  },
  getItem(id) {
    var that = this;
    var items = that.data.items;
    for (var i = 0; i < items.length; i++) {
      if (id == items[i].id) {
        return items[i];
      }
    }
  },
  onAgree(e) {
    //dd.alert({ title: e.currentTarget.id });
    var item = this.getItem(e.currentTarget.id);
    app.globalData.item = item;
    dd.navigateTo({
      url: '../../pages/form/form?type=1&single=true',
      success: function(res) {
      },
      fail: function(err) {
        dd.alert({ content: JSON.stringify(err) });
      },
      complete: function(res) {
      }
    });
  },
  onRefuse(e) {
    //dd.alert({ title: e.currentTarget.id });
    var item = this.getItem(e.currentTarget.id);
    app.globalData.item = item;
    dd.navigateTo({
      url: '../../pages/form/form?type=0&single=true',
      success: function(res) {
      },
      fail: function(err) {
      },
      complete: function(res) {
      }
    });
  },
  openfile(e) {
    var value = e.currentTarget.id;
    var arr = value.split('|');
    var url = arr[0];
    var name = arr[1];
    dd.saveFileToDingTalk({
      url: url,  // 文件在第三方服务器地址
      name: name,
      success: (res) => {
        var file = res.data[0];
        dd.previewFileInDingTalk({
          corpId: "ding474fbe9440dde2a735c2f4657eb6378f",
          spaceId: file.spaceId,
          fileId: file.fileId,
          fileName: file.fileName,
          fileSize: file.fileSize,
          fileType: file.fileType
        })
      },
      fail: (err) => {
        dd.alert({
          content: JSON.stringify(err)
        })
      }
    })
  },
  onSubmit() {
    //dd.alert({content:JSON.stringify(this.data.selectIdList)});
    if (this.data.selectIdList.length == 0) {
      dd.alert({ content: '请选中需要操作的审批单' });
    }
    else {
      var select_items = [];
      var list = this.data.selectIdList;
      for (var i = 0; i < list.length; i++) {
        var item = this.getItem(list[i]);
        select_items.push(item);
      }
      app.globalData.items = select_items;
      dd.navigateTo({
        url: '../../pages/form/form?type=1&single=false',
        success: function(res) {

        },
        fail: function(err) {

        },
        complete: function(res) {

        }
      });
    }
  },
});
